package model

import (
	"box-ma-fresh/boxrpc/boxrpc"
	"gorm.io/gorm"
)

type BoxGoods struct {
	gorm.Model
	Tid     int64   `gorm:"type:int(11);comment:商品类型"`
	Title   string  `gorm:"type:varchar(20);not null;comment:商品标题"`
	Price   float64 `gorm:"type:decimal(10,1);not null;comment:商品价格"`
	Content string  `gorm:"type:varchar(20);not null;comment:商品简介"`
}

func (g *BoxGoods) BoxGoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *BoxGoods) BoxGoodsDetail(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(g).Error
}

func (g *BoxGoods) GoodeDelById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id =? ", id).Delete(g).Error
}
func GoodsList(db *gorm.DB, in *boxrpc.GoodsListRequest) []*boxrpc.GoodsList {
	var list []*boxrpc.GoodsList
	db.Model(&BoxGoods{}).
		Select(
			"tid",
			"title",
			"price",
			"content",
		).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
